<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Msdk extends CI_Controller {
	public function index(){

		echo 111;
	}
	
	public function get_str($data = ''){
		$siteid = $this->input->post('siteid');
		$key = $this->get_encrypt($siteid);		
		$data = $data?$data:$this->input->post('the_data');		
		$data = str_replace(' ', '+', $data);			
		$str = $this->encrypt->decode($data, $key);
		return json_decode($str);
	}
	
	///返回密钥
	public function site_encrypt(){
		$siteid = $this->input->post('siteid');
		
		$this->load->model('website_model', 'wm');		
		echo $this->wm->getencrypt($siteid);		
	}
	
	function get_encrypt($siteid){
		$this->load->model('website_model', 'wm');
		
		return $this->wm->getencrypt($siteid);
	}
	//=======================================网站管理=============================//
	public function spread_order_num(){
		$a = array();
		$num = 24*60*60;
		for($i=7;$i>=1;$i--){
			$sql = "select * from sdk_spread_order where create_time>".(time()-$num*$i)." and create_time<".(time()-$num*($i-1));
			$q = $this->db->query($sql);
			$a[] = $q->num_rows();
		}
		
		echo json_encode($a);
	}
	
	
	public function site_list(){
		
		$siteid = $this->input->post('siteid');
		
		if($siteid="23456789"){
			
			$a = array();
			$sdk_member = $this->get_str();
			$mid = $sdk_member->mid;
			$this->load->model('spreaduser_model', 'sm');
			$re = $this->sm->get_s_sites($mid);
			/* $q = $this->sm->get_relationship($mid);
			$q2 = $this->sm->site_list();
			$a['checkedsite'] = $q;
			$a['allsite'] = $q2;
			
			$sites = array();
			$a['unchecksite'] = '';
			foreach ($q['sites'] as $k=>$v){
				$sites[] = $v->siteid;
			}
			foreach ($q2 as $k=>$v){
				if(in_array($v->siteid,$sites)){
					continue;
				}
				$a['unchecksite'][] = $v;
			} */

			echo json_encode($re);
		}
	}
	
	public function relationship(){
		
		$sdk_member = $this->get_str();
		
		$mid = $sdk_member->mid;
		$siteid = $sdk_member->siteid;
		
		$q = $this->db->get_where('sdk_spread_user',array('mid'=>$mid));
		$re = $q->row();
		$token = $re->token;
		
		$q = $this->db->get_where('sdk_site',array('siteid'=>$siteid));
		$re = $q->row();
		$sitename = $re->sitename;
		
		$config = array(
				'token'=>$token
		);
		
		$data = serialize(json_encode($config));
		$this->load->library('myclass');
		$r = $this->myclass->v_post('http://'.$sitename.'/change_token', 'the_data=' . $data);
		
		if($r == '-1'){
			$ack = -1;
			echo json_encode(array('ack'=>$ack));
			exit;
		}
		
		$this->load->model('spreaduser_model', 'sm');
		$ack = $this->sm->insert_relationship($sdk_member)?0:-1;
		
		echo json_encode(array('ack'=>$ack));
	}
	
	public function spread_order_list(){
		
		$spreadinfo = $this->get_str();
		$mid = $spreadinfo->mid;
		if($spreadinfo->sdate){
			$qe = explode('/', $spreadinfo->sdate);
			$sdate = mktime(0,0,0,$qe[1],$qe[0],$qe[2]);
		}else{
			$sdate = '';
		}
		if($spreadinfo->edate){
			$qe = explode('/', $spreadinfo->edate);
			$edate = mktime(0,0,0,$qe[1],$qe[0],$qe[2]);			
		}else{
			$edate = '';
		}
		$curr_page = $spreadinfo->curr_page;

		$this->load->model('spreaduser_model', 'sm');
		$showorder = $this->sm->sp_show_order($mid,$sdate,$edate,true,$curr_page-1);
		
		$totalorder = $this->sm->sp_order_total($mid,$sdate,$edate);
		
		$ttnum = count($this->sm->sp_show_order($mid,$sdate,$edate,false));
		
		$ttm = array(
				'usd' => 0,
				'gbp' => 0,
				'eur' => 0
		);
		
		foreach ($totalorder as $k=>$v){
			
			if($v->currency == 'USD') $ttm['usd'] += $v->total;
			if($v->currency == 'GBP') $ttm['gbp'] += $v->total;
			if($v->currency == 'EUR') $ttm['eur'] += $v->total;
		}
		
		$a = array(
				'orders' => $showorder,
				'ttm' => $ttm,
				'ttnum' => $ttnum
		);
		
		echo json_encode($a);
		
	}
	
	//************忘记密码*************************
	public function forget_pwd(){
		$sdk_address = $this->get_str();

		$this->load->model('member_model', 'mm');
		$this->load->library('myclass');
		
		$password = $this->myclass->randomkeys(8);
		$this->mm->forget_password($sdk_address, md5($password));
		
		//获取邮件配置		
		$q = $this->mm->get_siteinfo($sdk_address);	

		$this->myclass->send_forget_email($q->first_row(), $password);
	}

	
	//**************修改密码********************//
	public function update_password(){
		$sdk_member = $this->get_str();
		
		$this->load->model('spreaduser_model','sm');
		
		$ack = $this->sm->update_member_password($sdk_member);
		
		echo json_encode(array('ack'=>$ack));
	}
	
	public function forget_password(){
		$sdk_member = $this->get_str();
	}
	
	
	//****************会员登录************************//
	public function user_login(){

		$sdk_login = $this->get_str();

		$this->load->model('spreaduser_model', 'sm');
	
		$query = $this->sm->member_login($sdk_login);
		
		$sdk_member = null;
		
		$sdk_member->ack = -1;//用户名密码错误
		
		if ($query){
			
			$password1 = $this->get_str($sdk_login->password);
			$password2 = $this->get_str($query->password);
			
			if($password1 ==$password2){
					
				if($query->isactive){
				
					$sdk_member = $query;
					$sdk_member->ack = 0;
				}else{
				
					$sdk_member->ack = -2;//未审核
				}
			}	
			
		}

		echo json_encode($sdk_member);
		//ack 0 success, -1 fulat
	}
	
	//*************注册用户**************
	public function register_user(){
	
		$sdk_member = $this->get_str();

		$this->load->model('spreaduser_model','sm');

		$ack = 0;
		if ($this->sm->is_exist_member($sdk_member->email))
		{ 	
			$ack = -1; 	//email 已经存在		
		}else{
			$ack = $this->sm->insert_member($sdk_member);
		}
		
		$uid = 0;
		if ($ack > 0){
			$uid = $ack;
			$ack = 0;
		}
		
		$sdk_member->mid = $uid;
		$sdk_member->ack = $ack;
		
		echo json_encode($sdk_member);

		//ack = 0 成功 ， -1 已存在， -2 插入失败 
	}
	//============================================会员结束====================================//

	public function withdrawal(){
		
		$sdk_member = $this->get_str();
		$mid = $sdk_member->mid;
		$status = 0;	
		echo json_encode($this->get_money_info($mid,$status));
		
	}
	
	public function dowithdrawal(){
		
		$sdk_member = $this->get_str();
		$mid = $sdk_member->mid;
		$a = $this->get_money_info($mid);
		if($a['usd']['money']||$a['gbp']['money']||$a['eur']['money']){
			foreach ($a['orderid'] as $k=>$v){
				$this->db->update('sdk_spread_order',array('status'=>1),array('orderid'=>$v));
			}
			$data = array(
					'mid' => $mid,
					'USD' => $a['usd']['money'],
					'GBP' => $a['gbp']['money'],
					'EUR' => $a['eur']['money'],
					'apptime' => time(),
					'status' => 0
			);			
			$this->db->insert('sdk_extract',$data);
			if($this->db->affected_rows()>0){				
				echo json_encode(array('ack'=>'0'));
			}else{
				echo json_encode(array('ack'=>'-1'));
			}
		}else{
			echo json_encode(array('ack'=>'-1'));			
		}
	}
	
	public function viewwithdeawal(){
		$sdk_member = $this->get_str();
		$mid = $sdk_member->mid;
		$q = $this->db->get_where('sdk_extract',array('mid'=>$mid));
		if($q->num_rows()>0){
			
			echo json_encode(array('ack'=>'0','wrecodes'=>$q->result()));
		}else{
			
			echo json_encode(array('ack'=>'-1'));
		}
		
	}
	
	public function withdrawalrecode(){
		$sdk_member = $this->get_str();
		$mid = $sdk_member->mid;
		$q = $this->db->get_where('sdk_extract',array('mid'=>$mid));
		if($q->num_rows()>0){
			
			echo json_encode(array('ack'=>'0','wrecodes'=>$q->result()));
		}else{
			
			echo json_encode(array('ack'=>'-1'));
		}
		
	}
	
	//获取提现金额
	private function get_money_info($mid = '',$status = ''){
		
		$this->load->model('spreaduser_model','sm');
		
		$a = array(
				'usd'=>array(
						'money'=>0,
						'order'=>array()
		),
				'gbp'=>array(
						'money'=>0,
						'order'=>array()
				),
				'eur'=>array(
						'money'=>0,
						'order'=>array()
				),
				'orderid'=>array()
		);
		
		$data = array();
		if($status == ''){
			$data = array(
					'mid'=>$mid,
					'status' => '0'
			);
		}else{
			$data = array(
					'mid'=>$mid,
					'status'=>$status
			);
		}
		$q = $this->db->get_where('sdk_spread_order',$data);

		if($q->num_rows()>0){
			foreach ($q->result() as $k=>$v){
				$query = $this->db->get_where('sdk_order',array('orderid'=>$v->orderid));
				if($query->num_rows()>0){
					$re = $query->row();
					if($re->status != 'Success') continue;					
					if(time()-$re->create_time<30*24*60*60) continue;
					$percentage = $this->sm->site_percentage($mid,$re->siteid);
					switch ($re->currency){
						case 'USD':
							$a['usd']['money'] += floor($re->total*$percentage*100)/100;
							$a['usd']['order'][] = $re;
							$a['orderid'][] = $re->orderid;
							break;
						case 'GBP':
							$a['gbp']['money'] += floor($re->total*$percentage*100)/100;
							$a['gbp']['order'][] = $re;
							$a['orderid'][] = $re->orderid;
							break;
						case 'EUR':
							$a['eur']['money'] += floor($re->total*$percentage*100)/100;
							$a['eur']['order'][] = $re;
							$a['orderid'][] = $re->orderid;
							break;
					}
				}
			}
		}
		
		return $a;
	}
	
	
	public function get_total_money(){
		
		$sdk_member = $this->get_str();
		$mid = $sdk_member->mid;
		
		$a = array();
		$a['un'] = $this->get_money_info($mid);   //未提
		$status = 1;	//已提
		$a['en'] = $this->get_money_info($mid,$status);
		
		foreach ($a as $key=>$val){
			foreach ($val as $k=>$v){
				foreach ($v as $keyword=>$value){
					if($keyword == 'order')
						unset($a[$key][$k][$keyword]);
				}				
			}
		}

		echo json_encode($a);
		
	}
	
	
	
	
}